package com.wx.core.process;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

/**
 * 微信 API、微信基本接口
 */
public class WxApi {

	private WxApi(){

	}

	// token 接口
	public static String TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

	// 获取二维码
	public static String QRCODE_CREATE = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s";

	// 上传临时多媒体资料接口
	public static String MEDIA_UPLOAD = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";

	// 客服发送消息
	public static String CUSTOM_SEND = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";

	// 新增永久素材
	public static String MATERIAL_UPLOAD = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s";

	// 创建菜单
	public static String MENU_CREATE = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";

	// 创建个性化菜单
	public static String MENU_PERSONAL_CREATE = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=%s";

	// 删除菜单
	public static String MENU_DELETE = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s";

	// 获取账号粉丝信息
	public static String GET_FANS_INFO = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN";

	// 批量获取粉丝基本信息
	public static String GET_BATCH_FANS = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=%s&lang=zh_CN";

	// 获取账号粉丝列表
	public static String GET_FANS_LIST = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s";

	// 网页授权OAuth2.0获取code
	public static String GET_OAUTH_CODE = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=%s&scope=%s&state=%s#wechat_redirect";

	// 网页授权OAuth2.0获取token
	public static String GET_OAUTH_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

	// 网页授权OAuth2.0获取用户信息
	public static String GET_OAUTH_USERINFO = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";

	// 上传图文消息
	public static String UPLOAD_NEWS = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s";

	// 分组群发接口
	public static String SEND_BY_GROUP = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s";

	// 根据OpenID列表群发
	public static String SEND_BY_OPENID = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=%s";

	// 发送模板消息
	public static String SEND_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";

	// 获取分组列表
	public static String GET_GROUP_LIST = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=%s";

	// 创建分组
	public static String GROUP_CREATE = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=%s";

	// 修改分组名
	public static String GROUP_UPDATE = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=%s";

	// 删除分组
	public static String GROUP_DELETE = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=%s";

	// 获取用户增减数据（getusersummary）
	public static String FANS_ADD_SUB = "https://api.weixin.qq.com/datacube/getusersummary?access_token=%s";

	// 获取累计用户数据（getusercumulate）
	public static String FANS_TOTAL = "https://api.weixin.qq.com/datacube/getusercumulate?access_token=%s";

	// 移动用户分组
	public static String GROUP_MEMBER_UPDATE = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=%s";

	// 批量移动用户分组
	public static String GROUP_BATCH_UPDATE = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=%s";

	// 删除永久素材
	public static String DEL_MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/del_material";

	// 删除永久素材
	public static String DEL_MATERIAL_V2 = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=%s";

	// 查询群发消息状态
	public static String MASS_STATUS_QUERY = "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token=%s";

	// 上传永久图文素材
	public static String MATERIAL_ADD_NEWS = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s";

	// 批量获取素材
	public static String BATCHGET_MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=%s";

	// 获取素材
	public static String GET_MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=%s";

	// 获取授权方菜单信息
	public static String GET_CURRENT_SELFMENU_INFO = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=%s";

	// 微信长地址转成短地址
	public static String GET_SHORTURL = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=%s";

	// 通过code换取授权的openid和access_token
	public static String GET_AUTHORIZER_OPENID_URL = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=%s&code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s";

	// 通过code换取openid和access_token
	public static String GET_OPENID_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

	// 获取当天的用户留言
	public static String GET_MESSAGE_URL = "https://api.weixin.qq.com/customservice/msgrecord/getrecord?access_token=%s";

	// 获取临时素材
	public static String GET_MATERIAL_TEMP = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s";

	// 预览群发
	public static String MASS_PREVIEW = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s";

	// 群发上传图片获取Url，主要在详情页使用
	public static String MASS_UPLOADIMG = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s";

	// 上传多媒体文件接口
	public static String MASS_UPLOAD = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s";

	// 获取客服基本信息
	public static String GETKFLIST = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=%s";

	// 获取在线客服接待信息
	public static String GETONLINEKFLIST = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=%s";

	// 添加客服
	public static String ADDKF = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=%s";

	// 修改客服
	public static String UPDATEKF = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=%s";

	// 删除客服
	public static String DELKF = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=%s";

	// 邀请绑定客服
	public static String INVITEBINDKF = "https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token=%s";

	// 创建客服会话
	public static String CREATESESSION = "https://api.weixin.qq.com/customservice/kfsession/create?access_token=%s";

	// 关闭客服会话
	public static String CLOSESESSION = "https://api.weixin.qq.com/customservice/kfsession/close?access_token=%s";

	// 上传客服头像
	public static String UPLOADHEADIMG = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=%s";

	// 下载多媒体文件接口
	public static String MEDIA_GET = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s";

	public static String KF_ADD = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=%s";

	public static String KF_SESSION = "https://api.weixin.qq.com/customservice/kfsession/create?access_token=%s";

	// 上传临时素材
	public static String MATERIAL_UPLOAD_TEMP = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";

	/**
	 * 群发预览功能接口
	 */
	public static String MASS_SEND_PREVIEW = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s";
	
	/**
	 * 批量打标签
	 */
	public static final String BATCH_TAGGING_URL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=";
	
	/**
	 * 批量为用户取消标签
	 */
	public static String BATCH_UNTAGGING_URL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=%s";
	
	// 批量为用户取消标签
	public static String getBatchTaggingUrl(String requestUrl,String accessToken) {
		return String.format(requestUrl, accessToken);
	}

	// 获取GET_OPENID_URL
	public static String getOpenidUrl(String requestUrl,String appid,String appSecret,String code){

		return String.format(GET_OPENID_URL,appid,appSecret,code);
	}

	// 获取GET_AUTHORIZER_OPENID_URL
	public static String getAuthorizerOpenidUrl(String requestUrl,String appid,String code,String componentAppid,String componentAccessToken){

		return String.format(GET_AUTHORIZER_OPENID_URL,appid,code,componentAppid,componentAccessToken);
	}

	// 获取通用接口
	public static String getRequestUrl(String requestUrl,String accessToken){

		return String.format(requestUrl,accessToken);
	}

	// 获取token接口
	public static String getTokenUrl(String appId,String appSecret){

		return String.format(TOKEN,appId,appSecret);
	}

	// 获取上传Media接口
	public static String getUploadMediaUrl(String token,String type){

		return String.format(MEDIA_UPLOAD,token,type);
	}

	// 获取菜单创建接口
	public static String getMenuCreateUrl(String accessToken){

		return String.format(MENU_CREATE,accessToken);
	}

	// 获取菜单删除接口
	public static String getMenuDeleteUrl(String accessToken){

		return String.format(MENU_DELETE,accessToken);
	}

	// 获取粉丝信息接口
	public static String getFansInfoUrl(String accessToken,String openid){

		return String.format(GET_FANS_INFO,accessToken,openid);
	}

	// 获取粉丝列表接口
	public static String getFansListUrl(String accessToken,String nextOpenId){

		if(nextOpenId == null){
			return String.format(GET_FANS_LIST,accessToken);
		} else{
			return String.format(GET_FANS_LIST + "&next_openid=%s",accessToken,nextOpenId);
		}
	}

	// 网页授权OAuth2.0获取code
	public static String getOAuthCodeUrl(String appId,String redirectUrl,String scope,String state){

		try{
			redirectUrl = URLEncoder.encode(redirectUrl,"UTF-8");
		} catch (UnsupportedEncodingException e){
			e.printStackTrace();
		}
		return String.format(GET_OAUTH_CODE,appId,redirectUrl,"code",scope,state);
	}

	// 网页授权OAuth2.0获取token
	public static String getOAuthTokenUrl(String appId,String appSecret,String code){

		return String.format(GET_OAUTH_TOKEN,appId,appSecret,code);
	}

	// 网页授权OAuth2.0获取用户信息
	public static String getOAuthUserinfoUrl(String accessToken,String openid){

		return String.format(GET_OAUTH_USERINFO,accessToken,openid);
	}

	// 获取上传图文消息接口
	public static String getUploadNewsUrl(String accessToken){

		return String.format(UPLOAD_NEWS,accessToken);
	}

	// 获取发送模板消息接口
	public static String getSendTemplateUrl(String accessToken){

		return String.format(SEND_TEMPLATE,accessToken);
	}
	
	public static String httpsRequestStr(String requestUrl,String requestMethod, String outputStr) {
		String result = null;
		
		try {
			TrustManager[] tm = { new JEEWeiXinX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());
			SSLSocketFactory ssf = sslContext.getSocketFactory();

			URL url = new URL(requestUrl);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.setSSLSocketFactory(ssf);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(requestMethod);
			if (null != outputStr) {
				OutputStream outputStream = conn.getOutputStream();
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}
			InputStream inputStream = conn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			inputStream.close();
			inputStream = null;
			conn.disconnect();
			return buffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return result;
		} 
	}
}